/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.facebook.presto.obs;

import com.obs.services.ObsClient;
import com.obs.services.internal.ServiceException;
import com.obs.services.model.AccessControlList;
import com.obs.services.model.GetObjectMetadataRequest;
import com.obs.services.model.GetObjectRequest;
import com.obs.services.model.ListObjectsRequest;
import com.obs.services.model.ObjectListing;
import com.obs.services.model.ObjectMetadata;
import com.obs.services.model.ObsObject;
import com.obs.services.model.PutObjectRequest;
import com.obs.services.model.PutObjectResult;
import com.obs.services.model.StorageClassEnum;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Date;

import static java.net.HttpURLConnection.HTTP_OK;

public class MockObsClient
        extends ObsClient
{
    private static final String STANDARD_OBJECT_KEY = "test/standard";
    private static final String GLACIER_OBJECT_KEY = "test/glacier";

    private int getObjectHttpCode = HTTP_OK;
    private int getObjectMetadataHttpCode = HTTP_OK;
    private GetObjectMetadataRequest getObjectMetadataRequest;
    private AccessControlList acl;
    private boolean hasGlacierObjects;
    private boolean hasHadoopFolderMarkerObjects;

    public MockObsClient()
    {
        super("testEndPoint");
    }

    public MockObsClient(String endPoint) {
        super(endPoint);
    }

    public void setGetObjectHttpErrorCode(int getObjectHttpErrorCode)
    {
        this.getObjectHttpCode = getObjectHttpErrorCode;
    }

    public void setGetObjectMetadataHttpCode(int getObjectMetadataHttpCode)
    {
        this.getObjectMetadataHttpCode = getObjectMetadataHttpCode;
    }

    public AccessControlList getAcl()
    {
        return this.acl;
    }

    public void setHasGlacierObjects(boolean hasGlacierObjects)
    {
        this.hasGlacierObjects = hasGlacierObjects;
    }

    public void setHasHadoopFolderMarkerObjects(boolean hasHadoopFolderMarkerObjects)
    {
        this.hasHadoopFolderMarkerObjects = hasHadoopFolderMarkerObjects;
    }

    public GetObjectMetadataRequest getGetObjectMetadataRequest()
    {
        return getObjectMetadataRequest;
    }

    @Override
    public ObjectMetadata getObjectMetadata(GetObjectMetadataRequest getObjectMetadataRequest)
    {
        this.getObjectMetadataRequest = getObjectMetadataRequest;
        if (getObjectMetadataHttpCode != HTTP_OK) {
            ServiceException exception = new ServiceException("Failing getObjectMetadata call with " + getObjectMetadataHttpCode);
            exception.setResponseCode(getObjectMetadataHttpCode);
            throw exception;
        }
        return null;
    }

    @Override
    public ObsObject getObject(GetObjectRequest getObjectRequest)
    {
        if (getObjectHttpCode != HTTP_OK) {
            ServiceException exception = new ServiceException("Failing getObject call with status code:" + getObjectHttpCode);
            exception.setResponseCode(getObjectHttpCode);
            throw exception;
        }
        return null;
    }

    @Override
    public PutObjectResult putObject(PutObjectRequest putObjectRequest)
    {
        this.acl = putObjectRequest.getAcl();
        return new PutObjectResult(putObjectRequest.getBucketName(), putObjectRequest.getObjectKey(), null, null, null, null);
    }

    public PutObjectResult putObject(String bucketName, String key, String content)
            throws UnsupportedEncodingException
    {
        PutObjectResult putObjectResult = new PutObjectResult(bucketName, key, null, null, StorageClassEnum.STANDARD, null);
        putObjectResult.setCallbackResponseBody(new ByteArrayInputStream(content.getBytes(Charset.forName("UTF-8"))));
        return putObjectResult;
    }

    @Override
    public ObjectListing listObjects(ListObjectsRequest listObjectsRequest)
    {
        ObjectListing listing = new ObjectListing.Builder().truncated(false).builder();

        ObsObject standard = new ObsObject();
        ObjectMetadata standardMetadata = new ObjectMetadata();
        standardMetadata.setObjectStorageClass(StorageClassEnum.STANDARD);
        standardMetadata.setLastModified(new Date());
        standard.setObjectKey(STANDARD_OBJECT_KEY);
        standard.setMetadata(standardMetadata);
        listing.getObjects().add(standard);

        if (hasHadoopFolderMarkerObjects) {
            ObsObject hadoopFolderMarker = new ObsObject();
            ObjectMetadata hasHadoopMetadata = new ObjectMetadata();
            hasHadoopMetadata.setObjectStorageClass(StorageClassEnum.STANDARD);
            hasHadoopMetadata.setLastModified(new Date());
            hadoopFolderMarker.setMetadata(hasHadoopMetadata);
            hadoopFolderMarker.setObjectKey("test/test_$folder$");
            listing.getObjects().add(hadoopFolderMarker);
        }

        if (hasGlacierObjects) {
            ObsObject glacier = new ObsObject();
            ObjectMetadata metadata2 = new ObjectMetadata();
            metadata2.setObjectStorageClass(StorageClassEnum.COLD);
            metadata2.setLastModified(new Date());
            glacier.setMetadata(metadata2);
            glacier.setObjectKey(GLACIER_OBJECT_KEY);
            listing.getObjects().add(glacier);

            ObsObject deepArchive = new ObsObject();
            ObjectMetadata metadata3 = new ObjectMetadata();
            metadata3.setObjectStorageClass(StorageClassEnum.DEEP_ARCHIVE);
            metadata3.setLastModified(new Date());
            deepArchive.setMetadata(metadata3);
            deepArchive.setObjectKey("test/deepArchive");
            listing.getObjects().add(deepArchive);
        }
        return listing;
    }

    @Override
    public void shutdown()
    {
    }
}
